package com.soup.lab.mybatis.service.impl;

import com.soup.lab.mybatis.dao.CompanyMapper;
import com.soup.lab.mybatis.dao.EmployeeMapper;
import com.soup.lab.mybatis.model.Company;
import com.soup.lab.mybatis.model.Employee;
import com.soup.lab.mybatis.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void establish() {
        Random ran = new Random();
        Company company = new Company();
        company.setId(ran.nextInt(Integer.MAX_VALUE));
        company.setEstablishDate(new Date());
        company.setName("xxx皮包公司");
        company.setType(2);

        List<Employee> employeeList = new ArrayList<>();
        int empId = ran.nextInt(Integer.MAX_VALUE);
        for (int i = 0; i < 10; i++) {
            Employee employee = new Employee();
            employee.setCompanyId(company.getId());
            employee.setId(empId+i);
            employee.setHiredate(new Date());
            employee.setJobNo(String.format("%4d", i+1));
            employee.setName("员工"+i);
            employeeList.add(employee);
        }
        companyMapper.insert(company);
        employeeMapper.insertBatch(employeeList);
    }
}
